package com.meituan.stock.biz.service.impl;

import com.meituan.stock.biz.model.bo.StockBO;
import com.meituan.stock.biz.model.converter.StockBOConverter;
import com.meituan.stock.biz.model.converter.StockDOConverter;
import com.meituan.stock.biz.service.StockService;
import com.meituan.stock.dao.mapper.StockMapper;
import com.meituan.stock.dao.model.StockDO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.support.TransactionTemplate;

import javax.annotation.Resource;

/**
 * @author ShenWenhao
 * @Created by ShenWenhao
 * @Date 2022/5/9 16:11
 */
@Service
public class StockServiceImpl implements StockService {

    @Resource
    private StockMapper stockMapper;

    @Resource
    private TransactionTemplate transactionTemplate;

    @Override
    public StockBO initStock(StockBO stockBO) {
        return transactionTemplate.execute(transactionStatus -> {
            StockDO stockDO = StockDOConverter.fromBO(stockBO);
            int effectedRow = stockMapper.initStock(stockDO);
            if(effectedRow != 1){
                transactionStatus.setRollbackOnly();
                throw new IllegalStateException("初始化库存失败!");
            }
            return StockBOConverter.fromDO(stockDO);
        });
    }

    @Override
    public String inStock(StockBO stockBO) {
        return transactionTemplate.execute(transactionStatus -> {
            StockDO stockDO = StockDOConverter.fromBO(stockBO);
            // 查询id对应库存
            StockDO queryStockDO = stockMapper.queryStockById(stockDO);
            if(queryStockDO == null){
                transactionStatus.setRollbackOnly();
                throw new IllegalStateException("该库存不存在!");
            }
            // 进行添加
            int effectedRow = stockMapper.inStock(stockDO);
            if(effectedRow != 1){
                transactionStatus.setRollbackOnly();
                throw new IllegalStateException("入库失败!");
            }
            return "入库成功!";
        });
    }

    @Override
    public String outStock(StockBO stockBO) {
        return transactionTemplate.execute(transactionStatus -> {
            StockDO stockDO = StockDOConverter.fromBO(stockBO);
            // 查询id对应库存
            StockDO queryStockDO = stockMapper.queryStockById(stockDO);
            if(queryStockDO == null){
                transactionStatus.setRollbackOnly();
                throw new IllegalStateException("该库存不存在!");
            }
            // 判断数量和金额是否符合标准
            if(queryStockDO.getAccount() < stockBO.getAccount() || queryStockDO.getMoney().compareTo(stockBO.getMoney()) < 0){
                transactionStatus.setRollbackOnly();
                throw new IllegalStateException("数量或金额不符合标准!");
            }
            // 进行减少
            int effectedRow = stockMapper.outStock(stockDO);
            if(effectedRow != 1){
                transactionStatus.setRollbackOnly();
                throw new IllegalStateException("出库失败!");
            }
            return "出库成功!";
        });
    }
}
